package com.slm.krstartimes.adapter;


/**
 * Created by lanou3g on 15/8/5.
 */


import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.slm.krstartimes.R;
import com.slm.krstartimes.utils.News;

import java.util.List;

public class ViewPagerAdapter extends PagerAdapter {
    private List<News> advs;
    private Context context;
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public ViewPagerAdapter(Context context, List<News> advs) {
        this.context = context;
        this.advs = advs;
        //获取imageLoader的实例
        imageLoader = ImageLoader.getInstance();
        //初始化imageloader
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));


        //在这里可以设置缓存目录
        //File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache");

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.load)
                .showImageOnFail(R.mipmap.load)
                .displayer(new RoundedBitmapDisplayer(10))//设置圆角图片
                .cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置按比例缩放
                .cacheOnDisc()
                        //.discCache(new UnlimitedDiscCache(cacheDir))
                .build();
    }

    public void addPic(List<News> list) {
        for (News hm : list) {

            advs.add(hm);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return advs.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView img = new ImageView(context);

        News item = advs.get(position);


        Log.i("TAG", item.getImage());
        imageLoader.displayImage(item.getImage(), img, options);

        container.addView(img);

        return img;
    }
}


